package com.duck.communication.controller;

import com.duck.common.domain.PagerPojo;
import com.duck.common.domain.Result;
import com.duck.common.utils.UserContext;
import com.duck.communication.domain.dto.GetCommentDto;
import com.duck.communication.domain.dto.SendCommentDto;
import com.duck.communication.domain.vo.GetCommentVo;
import com.duck.communication.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;


    /**
     *  发送评论
     * @param comment 评论
     * @return 状态码
     */
    @PostMapping("/sendComment/")
    public Result sendComment(@RequestBody SendCommentDto comment){
        String userId = String.valueOf(UserContext.getUser());
        HashMap<String,Object> newComment = new HashMap<>(){{
            put("userId",userId);
            put("postId",comment.getPostId());
            put("content",comment.getContent());
            put("parentCommentId",comment.getParentCommentId());
            put("rootCommentId",comment.getRootCommentId());
        }};
        Integer result = commentService.addComment(newComment);
        if (result == 1) {
            return new Result(200, "添加成功");
        }
        return new Result(404, "添加失败");
    }


    /**
     * 查看对应帖子的评论
     * @param getCommentDto 入参
     * @return 评论树，对顶级评论分页返回
     */
    @PostMapping("/getComment/")
    public Result getComment(@RequestBody GetCommentDto getCommentDto) {
        PagerPojo<GetCommentVo> result = commentService.getComment(getCommentDto);
        return new Result(result,200,"查询成功");
    }

    /**
     * 返回评论总数
     * @param postId 帖子ID
     * @return 状态码
     */
    @GetMapping("/countCollect/")
    public Result countCollect(@RequestParam String postId){
        Integer result = commentService.commentCount(postId);
        return new Result(result,200,"查询成功");
    }
}
